package impl.search;

import anaydis.search.SymbolTable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by IntelliJ IDEA.
 * User: Tomi
 * Date: 09/11/2011
 * Time: 12:24:21
 * To change this template use File | Settings | File Templates.
 */
public abstract class AbstractOrderedArray<K, V> implements SymbolTable<K, V> {
    public ArrayList<EntryKV<K, V>> A;
    private int size;
    public AbstractOrderedArray() {
        A = new ArrayList<EntryKV<K, V>>();
        size = A.size();
    }
    public int size() {
        return size;
    }
    public boolean isEmpty() {
        return size==0;
    }
    public boolean containsKey(K k) {
        return search(k)!=-1;
    }
    public V get(K k) {
        int i = search(k);
        if(i!=-1){
            return (A.get(i).getValue());
        }
        return null;
    }
    public V put(K k, V v) {
        int i = search(k);
        if(i!=-1){
            return (V)(A.get(i).setValue(v));
        }
        for(int j=0; j<size; j++){
            int h = A.get(j).getKey().hashCode();
            if(h>k.hashCode()){
                A.add(j, new EntryKV(k, v));
                size++;
                return null;
            }
        }
        A.add(new EntryKV(k, v));
        size++;
        return null;
    }
    public void clear() {
        A = new ArrayList<EntryKV<K, V>>();
        size = 0;
    }
    //FALTA
    public Set entrySet() {
        return null;
    }
    public int search(Object key) {
        return search(key, 0, size-1);
    }
    public abstract int search(Object key, int i, int f);
}